﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;


namespace OPM.Class
{
    public static class Util
    {
        public static string RenderTagMetaRedirect(string url, string toPageName, string default_language)
        {
            string styleHtml = @"<style>
html{
    background: url(../images/bg.jpg) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

a{
    color:Yellow;
    font-size:30px; 
    font-family: supermarket, Arial, Tahoma !important;
}

@font-face {
  font-family: 'supermarket';
  src: url('../font/supermarket.eot');
  src: url('../font/supermarket.eot?#iefix') format('embedded-opentype'),
       url('../font/supermarket.woff2') format('woff2'),
       url('../font/supermarket.woff') format('woff'),
       url('../font/supermarket.ttf') format('truetype'),
       url('../font/supermarket.svg#supermarket') format('svg');
  font-weight: normal;
  font-style: normal;
}

</style>";
            string redirecPage = "";
            if (default_language == "En")
            {
                redirecPage = "Now turning to page...";
            }
            else
            {
                redirecPage = "กำลังเปลี่ยนหน้าไปที่";
            }


            return "<meta charset='UTF-8'><meta http-equiv='refresh' content='0.1; url=" + url +
                   "' /><center><a href='" + url + "' >" + redirecPage + " " + toPageName + "</a></center>" +
                   styleHtml;
        }

        public static string GetClientIp()
        {
            return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        public static string TryCutString(string s, string spliter, int position = 0)
        {
            string[] sSplited = (s ?? "").Split(new[] { spliter }, StringSplitOptions.None);
            return sSplited.Count() > position ? sSplited[position] : "";
        }

        public static string ConvertDateTimeToWebService(string datetime)
        {
            DateTime dt ;
            DateTime.TryParse(datetime, out dt);
            if (dt == DateTime.MinValue)
            {
                return "";
            }
            return ConvertDateTimeToWebService(dt);
        }

        public static string ConvertDateTimeToWebService(DateTime? datetime)
        {
            if (!datetime.HasValue) return null;
            return datetime.Value.ToString("o");
        }

        public static int? ConvertToIntNull(string s)
        {
            int result;
            if (Int32.TryParse(s, out result))
            {
                return result;
            }
            return null;
        }

        public static int ConvertToInt(string s, int defaultValue = 0)
        {
            int result;
            if (Int32.TryParse(s, out result))
            {
                return result;
            }
            return defaultValue;
        }

        public static void GetPagingData(string pageNumber, int amountRecord,
            out int pageRecordStart, out int pageRecordEnd, out int pageAmount,
            out int pageNo, out int prevPageNo, out int nextPageNo)
        {
            pageAmount = amountRecord / U.RowPerPage;

            // ตัวอย่าง page = 1, amount = 15, start = 1, end = 10, rowPerPage = 10
            if (ConvertToIntNull(pageNumber) == null && (string.IsNullOrWhiteSpace(pageNumber) || pageNumber == "F"))
            {
                pageNo = 1;
            }
            else if (ConvertToIntNull(pageNumber) == null && pageNumber == "L")
            {
                pageNo = pageAmount;
            }
            else
            {
                pageNo = ConvertToIntNull(pageNumber) ?? 1;
                if (pageNo < 1) pageNo = 1;
                if (pageNo > pageAmount) pageNo = pageAmount;
            }

            pageRecordEnd = U.RowPerPage * pageNo;

            pageRecordStart = pageRecordEnd - U.RowPerPage + 1;

            if (pageNo - 1 < 1)
            {
                prevPageNo = 1;
            }
            else
            {
                prevPageNo = pageNo - 1;
            }

            if (pageNo + 1 > pageAmount)
            {
                nextPageNo = pageAmount;
            }
            else
            {
                nextPageNo = pageNo + 1;
            }

            pageRecordStart = pageRecordStart <= amountRecord ? pageRecordStart : amountRecord;
            pageRecordEnd = pageRecordEnd <= amountRecord ? pageRecordEnd : amountRecord;
        }

    }
}